package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.entity.Role;
import com.lkd.entity.User;
import com.lkd.mapper.RoleMapper;
import com.lkd.mapper.UserMapper;
import com.lkd.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.PageBean;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageBean<UserVo> search(String userName, Integer pageIndex, Integer pageSize) {

        Page<User>page = new Page<>(pageIndex,pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        page=getBaseMapper().selectPage(page,wrapper);

        List<UserVo> userVoList =page.getRecords().stream().map(user -> {
            UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);

            LambdaQueryWrapper<Role> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Role::getRoleId,user.getRoleId());
            Role role = roleMapper.selectOne(wrapper1);


            userVo.setRole(role);

            return userVo;
        }).collect(Collectors.toList());

        return new PageBean<>(page,userVoList);
    }
}
